package com.cskaoyan.service.wx.brand;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: erdonc
 * @date: 2022/9/11 14:55
 * TODO: 展示品牌商列表
 */
@Service
public class WxBrandServiceImpl implements WxBrandService {

    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public CommonData<MarketBrand> list(BasePageInfo pageInfo) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        String sort = pageInfo.getSort();
        String order = pageInfo.getOrder();
        if (sort != null && order != null) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(false);
        long total = brandMapper.countByExample(example);
        Integer limit = pageInfo.getLimit();
        Integer pages = Math.toIntExact((total / limit) + 1);
        // 开启分页
        PageHelper.startPage(pageInfo.getPage(), limit);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);

        PageInfo<MarketBrand> brandPageInfo = new PageInfo<>(marketBrands);
        return CommonData.data(brandPageInfo);
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/11 15:32
     * TODO: 获取品牌详情页
     */
    @Override
    public MarketBrand detail(Integer id) {
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }

}
